package com.fone.player.activity.personal.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.entity.OfflineCache;

/**
 * 收藏管理界面 adapter (在我的收藏-->删除按钮激活后使用该adapter) 该界面下除对应itemClick可相应,其他空间相应均失效!
 * 
 * @author JasonZue
 * @since 2014-4-21
 */
public class CollectionManageListAdatper extends BaseAdapter {

	private List<OfflineCache> gridData;
	private Context mContext;

	public CollectionManageListAdatper(Context mContext,
			List<OfflineCache> gridData) {
		this.mContext = mContext;
		this.gridData = gridData;

	}

	@Override
	public int getCount() {
		return gridData.size();
	}

	@Override
	public Object getItem(int position) {
		return gridData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.personal_collections_list_item, null);

			holder.selectedCb = (CheckBox) convertView
					.findViewById(R.id.cache_is_selected_cb);
			holder.downloadStatusTv = (TextView) convertView
					.findViewById(R.id.collection_listview_item_download_status_tv);
			holder.seasonInfo = (TextView) convertView
					.findViewById(R.id.collection_listview_item_season_tv);
			holder.collectionType = (TextView) convertView
					.findViewById(R.id.collection_listview_item_collection_type_tv);
			holder.upDateInfo = (TextView) convertView
					.findViewById(R.id.collection_listview_item_update_info_tv);
			holder.mCacheingTv = (ImageView) convertView
					.findViewById(R.id.collection_is_in_cached);
			holder.itemImage = (ImageView) convertView
					.findViewById(R.id.collection_listview_item_pic_iv);
			holder.itemName = (TextView) convertView
					.findViewById(R.id.collection_listview_item_name_tv);
			holder.downloadProgressBar = (ProgressBar) convertView
					.findViewById(R.id.collection_listview_item_progressbar_pb);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 设置checkbox显示颜色
		holder.selectedCb
				.setBackgroundResource(R.drawable.collection_delete_checkbox_selector);
		holder.selectedCb.setVisibility(View.VISIBLE);
		holder.selectedCb.setClickable(false);

		if (null != gridData.get(position)) {

			holder.itemName.setText(gridData.get(position).getCacheName());
			holder.seasonInfo.setText(gridData.get(position)
					.getCacheEpisodeNum());
			holder.downloadProgressBar.setVisibility(View.GONE);
			holder.upDateInfo.setText(gridData.get(position)
					.getCacheEpisodeUpdateInfo());

			// 设置图片
			if (!TextUtils.isEmpty(gridData.get(position).getCacheImageUrl())) {
				ImageDownloadModule.getInstance().download(
						gridData.get(position).getCacheImageUrl(),
						holder.itemImage);
			}

			holder.selectedCb.setChecked(gridData.get(position)
					.getCacheIsDelete());

			// 修改图标及字体颜色
			holder.downloadStatusTv.setVisibility(View.GONE);
			if(gridData.get(position).getCacheIsDownlaod()){
				holder.mCacheingTv.setVisibility(View.VISIBLE);
			}else {
				holder.mCacheingTv.setVisibility(View.INVISIBLE);
			}

			switch (gridData.get(position).getCacheContentType()) {
			case OfflineCache.CACHE_CONTENT_TYPE_TV:
				holder.collectionType.setText("电视剧");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_WEB_PAGE:
				holder.collectionType.setText("网页");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_MOVIE:
				holder.collectionType.setText("电影");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_ANIMATION:
				holder.collectionType.setText("动漫");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_SHORT_FILM:
				holder.collectionType.setText("短剧");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_NEWS:
				holder.collectionType.setText("新闻");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_AUDIO:
				holder.collectionType.setText("直播音频");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_VIDEO:
				holder.collectionType.setText("直播");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_ORIGINAL:
				holder.collectionType.setText("原创");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_VARIETY:
				holder.collectionType.setText("综艺");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_OTHER:
				holder.collectionType.setText("其他");
				break;
			default:
				holder.collectionType.setText("未知");
				break;
			}
		}

		holder.downloadStatusTv.setClickable(false);

		return convertView;
	}

	class ViewHolder {
		public TextView itemName, downloadStatusTv, seasonInfo, upDateInfo,
				collectionType;
		public ImageView itemImage, itemNew;
		public CheckBox selectedCb;
		public ProgressBar downloadProgressBar;
		public ImageView mCacheingTv;
	}

	/**
	 * 修改下载状态
	 * 
	 * @param holder
	 * @param drawableId
	 *            图片R文件
	 * @param textColor
	 *            下载文字颜色R文件
	 * @param statusText
	 *            下载文字内容 String格式
	 * @param isShow
	 *            是否显示图标
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("unused")
	private void changeStatus(final ViewHolder holder, int drawableId,
			int textColor, String statusText, boolean isShow) {
		Drawable drawable = mContext.getResources().getDrawable(drawableId);

		holder.downloadStatusTv.setText(statusText);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		if (isShow) {
			holder.downloadStatusTv.setCompoundDrawables(null, drawable, null,
					null);
		} else {
			holder.downloadStatusTv
					.setCompoundDrawables(null, null, null, null);
		}
		holder.downloadStatusTv.setTextColor(mContext.getResources().getColor(
				textColor));
	}

}
